package com.jn.erp.wechat.staff.api.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.jn.erp.common.annotation.Log;
import com.jn.erp.common.core.result.AjaxResult;
import com.jn.erp.common.enums.BusinessType;
import com.jn.erp.web.api.course.entity.TCourseOrder;
import com.jn.erp.web.api.course.entity.TCourseOrderConfig;
import com.jn.erp.web.api.course.entity.dto.TCourseOrderConfigDto;
import com.jn.erp.web.api.course.entity.dto.TCourseOrderDto;
import com.jn.erp.web.api.course.entity.vo.TCourseOrderConfigVo;
import com.jn.erp.web.api.course.entity.vo.TCourseOrderVo;
import com.jn.erp.web.api.course.service.TCourseOrderConfigService;
import com.jn.erp.web.api.course.service.TCourseOrderService;
import com.jn.erp.web.api.student.entity.StudentClass;
import com.jn.erp.web.api.student.entity.TStudentClassRela;
import com.jn.erp.web.api.student.entity.dto.StudentClassDto;
import com.jn.erp.web.api.student.entity.dto.StudentListDto;
import com.jn.erp.web.api.student.entity.vo.StudentClassVo;
import com.jn.erp.web.api.student.service.StudentClassService;
import com.jn.erp.wechat.common.api.controller.base.BaseWxAuthController;
import com.jn.erp.wechat.core.WxTableDataInfo;
import com.jn.erp.wechat.common.api.entity.dto.WxUserDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * <pre>
 * 班级 相关接口管理
 * </pre>
 *
 * @author sunjiyong
 * @since 2024/9/29 19:52
 */
@RestController
@RequestMapping("/wx/class")
public class MpClassController extends BaseWxAuthController {

    @Autowired
    private StudentClassService studentClassService;
    @Autowired
    private TCourseOrderService tCourseOrderService;
    @Autowired
    private TCourseOrderConfigService tCourseOrderConfigService;

    //-------------------------------------班级---------------------
    /**
     * 班級列表  /tClass/list
     * */
    @GetMapping("/list")
    public WxTableDataInfo list(StudentClassVo studentClassVo) {
        WxUserDto wxUser = getWxUser();
        //机构id
        studentClassVo.setOrgId(wxUser.getOrgIdLong());
        //当前登录人id
        studentClassVo.setCreator(getUserId());
        if(!Objects.isNull(studentClassVo.getIfMine()) && studentClassVo.getIfMine()==0){
            studentClassVo.setMine(getUserId());
        }
        startPage();
        List<StudentClassDto> list = studentClassService.classList(studentClassVo);
        return getWxDataTable(list);
    }


    /**
     * 班級新增    /tClass/save
     * */
    @Log(title = "新增班級", businessType = BusinessType.INSERT)
    @PostMapping("/save")
    public AjaxResult save(@RequestBody StudentClassVo studentClassVo) {
        WxUserDto wxUser = getWxUser();
        //机构id
        studentClassVo.setOrgId(wxUser.getOrgIdLong());
        //当前登录人id
        studentClassVo.setCreator(wxUser.getUserId());
        studentClassVo.setDeleted(0);
        String code = studentClassService.saveClass(studentClassVo);
        return AjaxResult.success(code);
    }

    //-------------------------------------班級详情---------------------
    /**
     * 班級详情    /tClass/getById
     * */
    @PostMapping("/getById")
    public AjaxResult getById(@RequestBody StudentClassVo studentClassVo) {
        WxUserDto wxUser = getWxUser();
        //机构id
        studentClassVo.setOrgId(wxUser.getOrgIdLong());
        //当前登录人id
        studentClassVo.setCreator(wxUser.getUserId());
        StudentClass studentClass = studentClassService.getClassById(studentClassVo);
        studentClass.setBeOver(null);
        return AjaxResult.success(studentClass);
    }


    //-------------------------------------排课信息---------------------
    /**
     * 排课列表     /tCourseOrder/list
     * */
    @GetMapping("/orderInfoList")
    public WxTableDataInfo orderInfoList( TCourseOrderConfigVo tCourseOrderConfigVo) {
        WxUserDto wxUser = getWxUser();
        tCourseOrderConfigVo.setOrgId(wxUser.getOrgIdLong());
        tCourseOrderConfigVo.setCreateUser(String.valueOf(wxUser.getUserId()));
        startPage();
        List<TCourseOrderConfigDto> list = tCourseOrderConfigService.courseOrderConfigList(tCourseOrderConfigVo);
        return getWxDataTable(list);
    }

    //-------------------------------------班级学员---------------------
    /**
     * 班级成员列表   /tClass/studentList
     * */
    @GetMapping("/studentList")
    public WxTableDataInfo studentList(StudentClassVo studentClassVo) {
        WxUserDto wxUser = getWxUser();
        studentClassVo.setOrgId(wxUser.getOrgIdLong());
        studentClassVo.setCreator(wxUser.getUserId());
        startPage();
        List<StudentListDto> studentClass = studentClassService.studentList(studentClassVo);
        return getWxDataTable(studentClass);
    }


    /**
     * 班级成员添加    /tClass/addStudentToClass
     * */
    @PostMapping("/addStudentToClass")
    public AjaxResult addStudentToClass(@RequestBody List<TStudentClassRela> tStudentClassRelas) {
        WxUserDto wxUser = getWxUser();
        tStudentClassRelas.stream().forEach(u->{
            //机构id
            u.setOrgId(wxUser.getOrgIdLong());
            //当前登录人id
            u.setCreator(wxUser.getUserId() );
            u.setAddTime(LocalDateTime.now());
            u.setDeleted(0);
        });
        String code = studentClassService.addStudentToClass(tStudentClassRelas,1);
        return success(code);
    }


    /**
     * 移至其他班移除本班   /tClass/removeStudent
     * */
    @PostMapping("/removeStudent")
    public AjaxResult removeStudent(@RequestBody TStudentClassRela tStudentClassRela) {
        WxUserDto wxUser = getWxUser();
        //机构id
        tStudentClassRela.setOrgId(wxUser.getOrgIdLong());
        //当前登录人id
        tStudentClassRela.setEditor(wxUser.getUserId() );
        tStudentClassRela.setEditTime(LocalDateTime.now() );
        String code = studentClassService.removeStudent(tStudentClassRela);
        return AjaxResult.success(code);
    }

    //-------------------------------------点名情况---------------------
    /**
     * 点名情况日期  (小程序新建)
     * */
    @GetMapping("/orderDateList")
    public AjaxResult orderDateList(TCourseOrderVo tCourseOrderVo) {
        WxUserDto wxUser = getWxUser();
        tCourseOrderVo.setOrgId(wxUser.getOrgIdLong());
        tCourseOrderVo.setCreateUser(String.valueOf(wxUser.getUserId()) );
        List<TCourseOrderDto> list = tCourseOrderService.courseOrderList(tCourseOrderVo);
        Map<String, List<TCourseOrderDto>> collect = list.stream().collect(Collectors.groupingBy(TCourseOrderDto::getClassDate));
        Map<String, String> dateMap = collect.keySet().stream().collect(Collectors.toMap(s -> s.replaceAll("-0", "-"), s -> "deep-spot"));
        return success(dateMap);
    }


    /**
     * 指定日期点名情况列表    /tCourseOrder/orderList
     * */
    @GetMapping("/orderList")
    public WxTableDataInfo orderList(TCourseOrderVo tCourseOrderVo) {
        WxUserDto wxUser = getWxUser();
        tCourseOrderVo.setOrgId(wxUser.getOrgIdLong());
        tCourseOrderVo.setCreateUser(String.valueOf(wxUser.getUserId()) );
        startPage();
        List<TCourseOrderDto> list = tCourseOrderService.courseOrderList(tCourseOrderVo);
        return getWxDataTable(list);
    }

    /**
     * 上课记录列表（缺课补课）    /tCourseOrder/orderListSecond
     * */
    @GetMapping("/orderListSecond")
    public WxTableDataInfo orderListSecond(TCourseOrderVo tCourseOrderVo) {
        startPage();
        WxUserDto wxUser = getWxUser();
        //机构id
        tCourseOrderVo.setOrgId(wxUser.getOrgIdLong() );
        //当前登录人id
        tCourseOrderVo.setCreateUser(String.valueOf(wxUser.getUserId()));
        List<TCourseOrderDto> tCourseOrderDtos= tCourseOrderService.orderListSecond(tCourseOrderVo);
        return getWxDataTable(tCourseOrderDtos);
    }

    /**
     * 补课详情  /tCourseOrder/repairDetail
     * */
    @PostMapping("/repairDetail")
    public AjaxResult repairDetail(@RequestBody TCourseOrderVo tCourseOrderVo) {

        startPage();

        WxUserDto wxUser = getWxUser();
        tCourseOrderVo.setOrgId(wxUser.getOrgIdLong());
        tCourseOrderVo.setCreateUser(String.valueOf(wxUser.getUserId()) );
        tCourseOrderVo.setCreateDate(LocalDateTime.now() );

        List<TCourseOrderDto> tCourseOrderList = tCourseOrderService.repairDetail(tCourseOrderVo);
        return AjaxResult.success(tCourseOrderList);
    }
    //-------------------------------------课次详情---------------------
    /**
     * 排课课次详情   /tCourseOrder/orderGetById
     * */
    @PostMapping("/getClassTimeById")
    public AjaxResult getClassTimeById(@RequestBody TCourseOrderVo tCourseOrderVo) {
        WxUserDto wxUser = getWxUser();
        tCourseOrderVo.setOrgId(wxUser.getOrgIdLong());
        tCourseOrderVo.setCreateUser(String.valueOf(wxUser.getUserId()) );

        TCourseOrderDto tCourseOrderDto= tCourseOrderService.getCourseOrderById(tCourseOrderVo);
        return AjaxResult.success(tCourseOrderDto);
    }


    /**
     * 课次详情学员列表  /tCourseOrder/orderStudentList
     * */
    @GetMapping("/classTimeStudentList")
    public WxTableDataInfo classTimeStudentList(TCourseOrderVo tCourseOrderVo) {
        WxUserDto wxUser = getWxUser();
        tCourseOrderVo.setOrgId(wxUser.getOrgIdLong());
        tCourseOrderVo.setCreateUser(String.valueOf(wxUser.getUserId()) );
        startPage();
        List<TCourseOrderDto> tCourseOrderDtos= tCourseOrderService.orderStudentList(tCourseOrderVo);
        return getWxDataTable(tCourseOrderDtos);
    }

    /**
     * 补课班级创建     /tClass/saveRepair
     * */
    @PostMapping("/saveRepair")
    public AjaxResult saveRepair(@RequestBody StudentClassVo studentClassVo) {
        WxUserDto wxUser = getWxUser();
        //机构id
        studentClassVo.setOrgId(wxUser.getOrgIdLong());
        //当前登录人id
        studentClassVo.setCreator(wxUser.getUserId());
        studentClassVo.setDeleted(0);
        StudentClass studentClass = studentClassService.getOne(new QueryWrapper<StudentClass>().eq("deleted", 0).eq("is_repair", 0).eq("course_id", studentClassVo.getCourseId()));
        if(Objects.isNull(studentClass)){
            studentClass = studentClassVo;
            studentClass.setName(studentClassVo.getCourseName()+"（补课班)");
            studentClass.setClassHour(1);
            studentClass.setIsRepair(0);
            studentClass.setBeOver(1L);
            studentClassService.save(studentClassVo);

        }
        return AjaxResult.success(studentClass);
    }


    //-------------------------------------课次详情-点名---------------------
    /**
     * 学生报读列表   /tClass/studentCourseList
     * */
    @GetMapping("/studentCourseList")
    public WxTableDataInfo studentCourseList(StudentClassVo studentClassVo) {
        WxUserDto wxUser = getWxUser();
        studentClassVo.setOrgId(wxUser.getOrgIdLong());
        studentClassVo.setCreator(wxUser.getUserId());
        startPage();
        List<StudentListDto> studentClass = studentClassService.studentCourseList(studentClassVo);
        return getWxDataTable(studentClass);
    }

    /**
     *  添加临时学员  /tCourseOrder/addStudent
     * */
    @Log(title = "添加临时学员", businessType = BusinessType.INSERT)
    @PostMapping("/addStudent")
    public AjaxResult addStudent(@RequestBody TCourseOrderVo tCourseOrderVo) {
        String code = tCourseOrderService.addStudent(tCourseOrderVo);
        return AjaxResult.success(code);
    }

    /**
     *  点名记录修改   /tCourseOrder/updateBatchOrder
     * */
    @Log(title = "编辑点名记录", businessType = BusinessType.UPDATE)
    @PostMapping("/updateBatchOrder")
    public AjaxResult updateBatchOrder(@RequestBody List<TCourseOrder> tCourseOrders) {
        WxUserDto wxUser = getWxUser();
        tCourseOrders.stream().forEach(u->{
            //机构id
            u.setOrgId(wxUser.getOrgIdLong());
            //当前登录人id
            u.setUpdateUser(String.valueOf(wxUser.getUserId()) );
            u.setUpdateDate(LocalDateTime.now() );
            u.setCallTime(LocalDateTime.now() );
        });
        String code = tCourseOrderService.updateBatchOrder(tCourseOrders);
        return AjaxResult.success(code);
    }


    /**
     *  课次记录修改 /tCourseOrder/updateOrder
     * */
    @Log(title = "课次记录修改", businessType = BusinessType.UPDATE)
    @PostMapping("/updateClassTime")
    public AjaxResult updateClassTime(@RequestBody TCourseOrder tCourseOrder) {
        boolean b = tCourseOrderService.update(tCourseOrder,new QueryWrapper<TCourseOrder>().eq("order_no",tCourseOrder.getOrderNo()));
        if(b){
            return success();
        }else{
            return error();
        }
    }


    /**
     *  课次记录删除  /tCourseOrder/deleteOrder
     * */
    @PostMapping("/deleteClassTime")
    public AjaxResult delete(@RequestBody TCourseOrderVo tCourseOrderVo) {
        WxUserDto wxUser = getWxUser();
        //机构id
        tCourseOrderVo.setOrgId(wxUser.getOrgIdLong() );
        //当前登录人id
        tCourseOrderVo.setUpdateUser(String.valueOf(wxUser.getUserId()) );
        tCourseOrderVo.setUpdateDate(LocalDateTime.now() );
        tCourseOrderVo.setIsDelete("1");
        if(!Objects.isNull(tCourseOrderVo.getOrderNo())) {
            tCourseOrderService.update(tCourseOrderVo,new QueryWrapper<TCourseOrder>().eq("order_no",tCourseOrderVo.getOrderNo()));
        }else if(!Objects.isNull(tCourseOrderVo.getId())){
            tCourseOrderService.updateById(tCourseOrderVo);
        }

        return AjaxResult.success("S200");
    }

    /**
     *  点名记录修改--标记已补   /tCourseOrder/updateOrderById
     * */
    @PostMapping("/updateOrderById")
    public AjaxResult updateOrderById(@RequestBody TCourseOrder tCourseOrder) {
        WxUserDto wxUser = getWxUser();
        //机构id
        tCourseOrder.setOrgId(wxUser.getOrgIdLong());
        //当前登录人id
        tCourseOrder.setUpdateUser(String.valueOf(wxUser.getUserId()) );
        tCourseOrder.setUpdateDate(LocalDateTime.now() );
        String code = tCourseOrderService.updateOrderById(tCourseOrder);
        return AjaxResult.success(code);
    }

    //-------------------------------------课次- 一键排课 ---------------------
    /**
     排课新增  /tCourseOrder/save
     */
    @PostMapping("courseOrderSave")
    public AjaxResult courseOrderSave(@RequestBody TCourseOrderConfigVo tCourseOrderConfigVo){
        WxUserDto wxUser = getWxUser();
        //机构id
        tCourseOrderConfigVo.setOrgId(wxUser.getOrgIdLong());
        //当前登录人id
        tCourseOrderConfigVo.setCreateUser(String.valueOf(wxUser.getUserId()) );
        tCourseOrderConfigVo.setIsDelete("0");
        Boolean boo = tCourseOrderConfigService.saveCourseOrder(tCourseOrderConfigVo);
        if(boo){
            return success();
        }else{
            return error();
        }
    }


    /**
     * 排课详情   /tCourseOrder/getById
     * */
    @PostMapping("/getCourseOrderConfigById")
    public AjaxResult getCourseOrderConfigById(@RequestBody TCourseOrderConfigVo tCourseOrderConfigVo) {
        WxUserDto wxUser = getWxUser();
        //机构id
        tCourseOrderConfigVo.setOrgId(wxUser.getOrgIdLong());
        //当前登录人id
        tCourseOrderConfigVo.setCreateUser(String.valueOf(wxUser.getUserId()) );
        TCourseOrderConfig tCourseOrderConfig = tCourseOrderConfigService.getCourseOrderConfig(tCourseOrderConfigVo);
        return AjaxResult.success(tCourseOrderConfig);
    }


    /**
     排课修改  /tCourseOrder/update
     */
    @PostMapping("courseOrderUpdate")
    public AjaxResult update(@RequestBody TCourseOrderConfigVo tCourseOrderConfigVo){
        WxUserDto wxUser = getWxUser();
        //机构id
        tCourseOrderConfigVo.setOrgId(wxUser.getOrgIdLong());
        //当前登录人id
        tCourseOrderConfigVo.setUpdateUser(String.valueOf(wxUser.getUserId()) );
        tCourseOrderConfigVo.setUpdateDate(LocalDateTime.now() );
        tCourseOrderConfigVo.setIsDelete("0");
        Boolean boo = tCourseOrderConfigService.updateCourseOrder(tCourseOrderConfigVo);
        return AjaxResult.success(boo);
    }


    /**
     排课删除   /tCourseOrder/delete
     */
    @PostMapping("courseOrderDelete")
    public AjaxResult delete(@RequestBody TCourseOrderConfigVo tCourseOrderConfigVo){
        WxUserDto wxUser = getWxUser();
        //机构id
        tCourseOrderConfigVo.setOrgId(wxUser.getOrgIdLong());
        //当前登录人id
        tCourseOrderConfigVo.setUpdateUser(String.valueOf(wxUser.getUserId()) );
        tCourseOrderConfigVo.setUpdateDate(LocalDateTime.now() );
        tCourseOrderConfigVo.setIsDelete("1");
        Boolean boo = tCourseOrderConfigService.updateCourseOrder(tCourseOrderConfigVo);
        return AjaxResult.success(boo);
    }

    /**
     * 开班补课点名     /tCourseOrder/saveRepair
     * */
    @PostMapping("/courseOrderSaveRepair")
    public AjaxResult saveRepair(@RequestBody TCourseOrderConfigVo tCourseOrderConfigVo) {
        WxUserDto wxUser = getWxUser();
        //机构id
        tCourseOrderConfigVo.setOrgId(wxUser.getOrgIdLong());
        //当前登录人id
        tCourseOrderConfigVo.setCreateUser(String.valueOf(wxUser.getUserId()) );
        tCourseOrderConfigVo.setCreateDate(LocalDateTime.now() );
        String code = tCourseOrderService.saveRepair(tCourseOrderConfigVo);
        return AjaxResult.success(code);
    }

//---------------------------------------------学员详情操作----------------------------------
    /**
     * 班级成员添加(学院页)    /tClass/addStudentToClassSecond
     * */
    @PostMapping("/addStudentToClassSecond")
    public AjaxResult addStudentToClassSecond(@RequestBody List<TStudentClassRela> tStudentClassRelas) {
        WxUserDto wxUser = getWxUser();
        tStudentClassRelas.stream().forEach(u->{
            //机构id
            u.setOrgId(wxUser.getOrgIdLong());
            //当前登录人id
            u.setCreator(wxUser.getUserId() );
            u.setAddTime(LocalDateTime.now());
            u.setDeleted(0);
        });
        String code = studentClassService.addStudentToClass(tStudentClassRelas,2);

        return AjaxResult.success(code);
    }
}
